package cn.com.blackboard.SISIntergrationBiz.util;

import org.apache.commons.dbcp.BasicDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import blackboard.persist.PersistenceException;
import blackboard.platform.context.ContextManager;
import blackboard.platform.context.ContextManagerFactory;
import blackboard.platform.context.UnsetContextException;
import blackboard.platform.vxi.data.VirtualInstallation;
import blackboard.platform.vxi.service.VirtualSystemException;

public class SISIDBCustDataSource extends BasicDataSource{
	
	private static final Logger logger = LoggerFactory.getLogger(SISIDBCustDataSource.class);
	
	SISIDBCustDataSource(){
		super();
		initDataSource();		
	}
	
    public void initDataSource(){
    	ContextManager cm = ContextManagerFactory.getInstance();
    	VirtualInstallation vi = null;
    	try {
			vi = cm.getContext().getVirtualInstallation();
		} catch (VirtualSystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (PersistenceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsetContextException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	logger.info("init datasource");
		String host = vi.getDbHost();  //app.bb.com
		String dbType = vi.getDbType(); //
		String pwd = vi.getDbPass();
		String user = vi.getDbUser();
		String insType = vi.getDbInstanceType();  //sid
		String ins = vi.getDbInstance();   //orcl
		String port = String.valueOf(vi.getDbPort()); //1521
		String driver = "";
		String url = "";
		if(dbType.equals("oracle")){
			if(insType.equals("SID")){
				url = "jdbc:oracle:thin:@"+host+":"+port+":"+ins;
			}
			else
				url = "jdbc:oracle:thin:@"+host+":"+port+"/"+ins;
			driver="oracle.jdbc.driver.OracleDriver";
		}
		else if(dbType.equals("mssql")){
			driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
			url = "jdbc:sqlserver://"+host+":"+port+";databaseName="+"SISINTERGRATIONTMP";
		}
		  
		super.setUrl(url);
		super.setDriverClassName(driver);

    	logger.info("init datasource finished");
	}
}
